;(function ($, window) {

    //从服务器上获取初始时间
    var currentDate =new Date(parseInt($('#curDate').text()));
    function dateRun() {
        currentDate.setSeconds(currentDate.getSeconds()+1);
        var time = "";
        var year = currentDate.getFullYear();
        var month = currentDate.getMonth() + 1;
        var day = currentDate.getDate();
        var hour = currentDate.getHours();
        var minute = currentDate.getMinutes();
        var second = currentDate.getSeconds();
        if(hour < 10){
            time += "0" + hour;
        }else{
            time += hour;
        }
        time += ":";
        if(minute < 10){
            time += "0" + minute;
        }else{
            time += minute;
        }
        time += ":";
        if(second < 10){
            time += "0" + second;
        }else{
            time += second;
        }
        document.getElementById("nowTime").innerHTML = year+"年"+month+"月"+day+"日" + time;
    }
    window.setInterval(dateRun, 1000);

    var $tab = $("#quartzTable").DataTable({
        'ajax': function (data, callback) {
            DyyBase.ajax(DyyBase.root_path + '/quartz/getAll.json', {}, function (data) {
                var result = {};
                result.data = data.result;
                callback(result);
            });
        },
        'drawCallback': function () {
        },
        'columns': [
            {
                data: "name", render: function (data) {
                return "<span title='" + data + "'>" + data + "</span>"
            }
            },
            {
                data: "group", render: function (data) {
                return "<span title='" + data + "'>" + data + "</span>"
            }
            },
            {
                data: "nextTime", render: function (data) {
                if(data == null) data = '-';
                return "<span title='" + data + "'>" + data + "</span>"
            }
            },
            {
                data: " prevTime", render: function (data) {
                if(data == null) data = '-';
                return "<span title='" + data + "'>" + data + "</span>"
            }
            },
            {
                data: " endTime", render: function (data) {
                if(data == null) data = '-';
                return "<span title='" + data + "'>" + data + "</span>"
            }
            },
            {
                data: "desc", render: function (data) {
                return "<span title='" + data + "'>" + data + "</span>"
            }
            },
            {
                data: "jobState", render: function (data) {
                return "<span title='" + data + "'>" + data + "</span>"
            }
            },
            {
                data: "cron", render: function (data) {
                return "<span title='" + data + "'>" + data + "</span>"
            }
            },
            {
                data: "timeZone", render: function (data) {
                return "<span title='" + data + "'>" + data + "</span>"
            }
            },
            {
                data: 'name',className : 'txtLeft', render: function (data,d,row) {
                var html = '';
                html += '<i title="删除任务" class="delBtn zy-delete-upload-icon"></i>&nbsp;&nbsp;';
                html +='<i class="startJobNow zy-see-icon" title="立即运行"></i>&nbsp;&nbsp;' +
                    '<i class="pauseJob zy-shenhe-icon" title="暂停任务"></i>&nbsp;&nbsp;' +
                    '<i class="resumeJob zy-jianyi-icon" title="恢复任务"></i>&nbsp;&nbsp;';
                if(row.cron) {
                    html += '<i title="修改cron" class="changeCron zy-record-add-icon" type="button"></i>&nbsp;&nbsp;';
                }
                return html;
            }
            }
        ],
        'info': false,
        'autoWidth': false,
        'serverSide': true,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'paging': false
    });

    $('#quartzTable').delegate('.delBtn', 'click', function () {
        var $tr = $(this).parent().parent();
        var data = $tab.row($tr).data();
        var group = data.group;
        var name = data.name;
        var param = {group: group, name: name};

        $(this).MSG({
            type: 'confirm',
            height: 230,
            content: '是否删除任务名为："' + name + '"，组名为："' + group + '"的任务？',
            yesBtnTxt: '确认',
            yesBtnAct: function () {
                DyyBase.ajax(DyyBase.root_path + '/quartz/deleteJob.json', param, function (d) {
                    if (d.bool) {
                        $(this).MSG({
                            content: d.result, yesBtnAct: function () {
                                $tab.draw();
                            }
                        });

                    } else {
                        $(this).MSG({content: d.result});
                    }
                });
            }
        });
    });

    $('#quartzTable').delegate('.changeCron', 'click', function () {
        var $tr = $(this).parent().parent();
        var data = $tab.row($tr).data();
        var group = data.group;
        var name = data.name;
        var param = {group: group, name: name};
        var cron = prompt("请输入新的cron表达式");
        if (cron) {
            param.cron = cron;
            DyyBase.ajax(DyyBase.root_path + '/quartz/modifyJobTime.json', param, function (d) {
                if (d.bool) {
                    $(this).MSG({
                        content: d.result, yesBtnAct: function () {
                            $tab.draw();
                        }
                    });
                } else {
                    $(this).MSG({content: d.result});
                }
            });
        }
    });

    $('#quartzTable').delegate('.pauseJob', 'click', function () {
        var $tr = $(this).parent().parent();
        var data = $tab.row($tr).data();
        var group = data.group;
        var name = data.name;
        var param = {group: group, name: name};
        $(this).MSG({
            type: 'confirm',
            height: 230,
            content: '是否暂停任务名为："' + name + '"，组名为："' + group + '"的任务？',
            yesBtnTxt: '确认',
            yesBtnAct: function () {
                DyyBase.ajax(DyyBase.root_path + '/quartz/pauseJob.json', param, function (d) {
                    if (d.bool) {
                        $(this).MSG({
                            content: d.result, yesBtnAct: function () {
                                $tab.draw();
                            }
                        });

                    } else {
                        $(this).MSG({content: d.result});
                    }
                });
            }
        });
    });


    $('#quartzTable').delegate('.resumeJob', 'click', function () {
        var $tr = $(this).parent().parent();
        var data = $tab.row($tr).data();
        var group = data.group;
        var name = data.name;
        var param = {group: group, name: name};

        $(this).MSG({
            type: 'confirm',
            height: 230,
            content: '是否恢复任务名为："' + name + '"，组名为："' + group + '"的任务？',
            yesBtnTxt: '确认',
            yesBtnAct: function () {
                DyyBase.ajax(DyyBase.root_path +'/quartz/resumeJob.json', param, function (d) {
                    if (d.bool) {
                        $(this).MSG({
                            content: d.result, yesBtnAct: function () {
                                $tab.draw();
                            }
                        });

                    } else {
                        $(this).MSG({content: d.result});
                    }
                });
            }
        });
    });

    $('#quartzTable').delegate('.startJobNow', 'click', function () {
        var $tr = $(this).parent().parent();
        var data = $tab.row($tr).data();
        var group = data.group;
        var name = data.name;
        var param = {group: group, name: name};

        $(this).MSG({
            type: 'confirm',
            height: 230,
            content: '是否立即运行任务名为："' + name + '"，组名为："' + group + '"的任务？',
            yesBtnTxt: '确认',
            yesBtnAct: function () {
                DyyBase.ajax(DyyBase.root_path +'/quartz/startJobNow.json', param, function (d) {
                    if (d.bool) {
                        $(this).MSG({
                            content: d.result, yesBtnAct: function () {
                                $tab.draw();
                            }
                        });

                    } else {
                        $(this).MSG({content: d.result});
                    }
                });
            }
        });
    });

    $('#initBtn').on('click', function () {
        DyyBase.ajax(DyyBase.root_path +'/quartz/initTask.json', {}, function (d) {
            if (d.bool) {
                $(this).MSG({
                    content: d.result, yesBtnAct: function () {
                        $tab.draw();
                    }
                });

            } else {
                $(this).MSG({content: d.result});
            }
        });
    });
    $('#deleteAllBtn').on('click', function () {
        $(this).MSG({
            type: 'confirm',
            height: 230,
            content: '是否删除所有任务？',
            yesBtnTxt: '确认',
            yesBtnAct: function () {
                DyyBase.ajax(DyyBase.root_path +'/quartz/deleteAllJob.json', {}, function (d) {
                    if (d.bool) {
                        $(this).MSG({
                            content: d.result, yesBtnAct: function () {
                                $tab.draw();
                            }
                        });

                    } else {
                        $(this).MSG({content: d.result});
                    }
                });
            }
        });
    });

    $('#deleteDefault').on('click', function() {
        $(this).MSG({
            type: 'confirm',
            height: 230,
            content: '是否删除系统默认任务？',
            yesBtnTxt: '确认',
            yesBtnAct: function () {
                DyyBase.ajax(DyyBase.root_path +'/quartz/deleteDefaultTask.json', {}, function (d) {
                    if (d.bool) {
                        $(this).MSG({
                            content: d.result, yesBtnAct: function () {
                                $tab.draw();
                            }
                        });

                    } else {
                        $(this).MSG({content: d.result});
                    }
                });
            }
        });
    });

    $('#refreshTab').on('click', function() {
        $tab.draw();
    });

})(jQuery, window);